<?php

namespace App\Admin\Controllers;

use App\Models\City;
use App\Models\Member\Member;
use App\Models\Nation;
use App\Models\Obj;
use App\Models\Org;
use App\Models\Work;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Grid\Displayers\Actions;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use SimpleSoftwareIO\QrCode\Facades\QrCode;


class WorkController extends AdminController
{
    protected $title = '帮扶工作目标管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Work());

        $grid->column('member.name', '姓名')->style('min-width:100px;');
        // 不存在的`full_name`字段
        $grid->column('years', '年份')->display(function () {
            return $this->year .'年';
        })->style('min-width:100px;');
        $grid->column('org.title', '所属单位')->style('min-width:100px;');
        $grid->target('目标数')->style('min-width:100px;')->default(4);
        $grid->finish('完成数')->style('min-width:100px;');

        $grid->disableExport();
        $grid->disableRowSelector();


        $grid->filter(function (Grid\Filter $filter) {
            $filter->expand();
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            $filter->column(1/3, function ($filter) {
                $filter->equal('year', '年份')->year();
            });

            $filter->column(1/3, function ($filter) {
                $filter->equal('org_id', '所属单位')->select(Org::where('type', 0)->pluck('title', 'id'));
            });
        });

        $grid->setActionClass(Actions::class);

        $grid->disableCreateButton();

        $grid->actions(function ($actions) {
            // 去掉删除
            $actions->disableDelete();
            // 去掉查看
            $actions->disableView();
        });

        return $grid;
    }

    protected function detail($id)
    {
        $show = new Show(Work::findOrFail($id));

        $show->field('member.name', '姓名');
        $show->field('year', '年份');
        $show->field('target', '目标数');
        $show->field('finish', '完成数');

        return $show;
    }



    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Work());

        $form->display('id', 'ID');
        $form->display('member.name', '姓名');
        $form->date('year', '年份')->format('YYYY');

        $form->number('target', '目标数')->max(1000);


        $form->tools(function (Form\Tools $tools) {
            // 去掉`删除`按钮
            $tools->disableDelete();
            // 去掉`查看`按钮
            $tools->disableView();
        });

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();

        });

        return $form;
    }
}
